package cn.e3mall.content.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.e3mall.common.pojo.EasyUITreeNode;
import cn.e3mall.common.util.E3Result;
import cn.e3mall.content.service.ContentCategoryService;
import cn.e3mall.mapper.TbContentCategoryMapper;
import cn.e3mall.pojo.TbContentCategory;
import cn.e3mall.pojo.TbContentCategoryExample;
import cn.e3mall.pojo.TbContentCategoryExample.Criteria;
/**
 * 内容分类管理Service
 * @author zhouzhixiang
 *
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

	@Autowired
	private TbContentCategoryMapper contentCategoryMapper;
	
	@Override
	public List<EasyUITreeNode> getContentCatList(long parentId) {
		 //根据parentid 查询子节点列表
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		//设置查询条件
		criteria.andParentIdEqualTo(parentId);
		//执行查询
		List<TbContentCategory> catlist = contentCategoryMapper.selectByExample(example);
		//转换成EasyUiTreeNode的列表
		List<EasyUITreeNode> nodelist = new ArrayList<EasyUITreeNode>();
		for (TbContentCategory tbContentCategory : catlist) {
			EasyUITreeNode node = new EasyUITreeNode();
			node.setId(tbContentCategory.getId());
			node.setState(tbContentCategory.getIsParent()?"closed":"open");
			node.setText(tbContentCategory.getName());
			nodelist.add(node);
		}
		
		return nodelist;
	}

	@Override
	public E3Result addContentCategory(long parentId, String name) {
		//创建TbContentCategory对象并补全属性
		TbContentCategory tCategory = new TbContentCategory();
		tCategory.setParentId(parentId);
		tCategory.setName(name);
		//1正常 0 删除
		tCategory.setStatus(1);
		//默认排序1
		tCategory.setSortOrder(1);
		//新添加的节点一定是叶子节点
		tCategory.setIsParent(false);
		tCategory.setCreated(new Date());
		tCategory.setUpdated(new Date());
		//插入数据库
		contentCategoryMapper.insert(tCategory);
		//判断父节点的isparent属性，如果不是true改为true 
		//根据parentid查询父节点
		TbContentCategory parent = contentCategoryMapper.selectByPrimaryKey(parentId);
		//根据parentid查询父节点
		if(!parent.getIsParent())
		{
			parent.setIsParent(true);
			//更新到数据库中
			contentCategoryMapper.updateByPrimaryKey(parent);
		}
		//返回结果
		return E3Result.ok(tCategory);
	}

	@Override
	public E3Result delContentCategory(long id) {
		//根据id得到这个TbContentCategory对象
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(id);
		 TbContentCategory contentCategory = contentCategoryMapper.selectByExample(example).get(0);
		 //判断其是否为叶子节点
		 if (contentCategory.getIsParent()) {
			 return E3Result.ok("1");
		 }
		 //删除节点
		 contentCategoryMapper.deleteByPrimaryKey(id);
		//得到其父节点
		 TbContentCategory parent = contentCategoryMapper.selectByPrimaryKey(contentCategory.getParentId());
		 //删除后判断起父节点下是否有子节点，没有则修改为叶子节点
		 if (contentCategoryMapper.selectHasChildren(id)!=1) {
			 parent.setIsParent(false);
			contentCategoryMapper.updateByPrimaryKeySelective(parent);
		 }
		return E3Result.ok();
	}

}
